K4: An old language for a new machine

I’ve been messing about with computers for a long time. I probably should be thinking about other things nowadays, but I can’t help my fascination with the things.

Now I’m coming back to my roots building embedded systems in my time at Bored Owl. As much as I love software, having a connection to actual hardware the switching of things and measuring stuff makes computers relevant and important. My projects have involved making new hardware and ultimately writing software to get that hardware going.

Embedded hardware has come a long way in 45 years. I started writing and manually loading assembly language programs - compilers were an expensive luxury in more ways than one. Not only did you have to afford the compiler and development syste but also generally larger and more expensive target hardware to run it on.

Now for a few tens of dollars and an evening on the internet, a 32bit 200MHz processor will run Micropython and empower you in so many ways.

But what about K4?

I started a project to build digital clocks using LED filaments called “Kairos”. This project uses the aforementioned 32 bit processor (an ESP32 Arduino Nano) to drive LED digit cards that we make. You can buy the digit cards in our shop, and these are discussed in the Kairos blog.

Initially, the clock software reached a point where I wanted to enable the user to use simple scripts to define some action that could happen at a specific time or times. The action was simply to play a chime from a sound file on an SD card. The more I thought about a suitable user interface the less appealing a GUI was: it was simply to limited. A proper script language would be better but I didn’t really want to design and document one.

About the same time, this ancient book about “FORTH” fell out of my shelves while I was clearing up:

A book bought in 1983 - out of curiosity.

Somewhat beaten up and well read over time.

This book also reminded me of running a similar language called “STOIC” (https://en.wikipedia.org/wiki/STOIC) on a Motorola 68000 CPU that I built between 1981 and 1982. Forth is somewhat better known but the concepts between the languages are very similar.

A 1980’s 68000 computer:

Made in my early university days, similar to and contemporary with and original Apple Macintosh 128K. Only this wasn’t graphic it was designed for music sampling and synthesis. It ran a port of “STOIC”.

Ok Ok Ok enough of the reminiscing! Back to a reality where I needed a lightweight language that could script some digital clocks. It didn’t take me long to open a new C++ project in Eclipse (the IDE I’m most familiar with, please don’t judge!).

K4 was born as a pure C++ implementation of a stack based language. This blog will gradually take you through the process and get you to a working implementation you can put on your desktop OS, or an ESP32 or an ARM or lots of other possibilities.

Sure there are real FORTH implementations out there. Far more “standard” maybe more what you want. What I wanted was a piece of code that I had the freedom to mold and shape and play with. And I wanted the pure satisfaction of the exercise.

What is a “Stack Based Language”?

The languages that get this description all use the concept of a “stack”. That’s a simple data structure that stores data in a first-in, last-out manner. Which tells you next to nothing! So I’ll give you some more details:

  • K4 is and “interactive compiler”. So is Python and Scala etc. You can enter commands at a command line and they will be executed immediately. If input tells K4 to compile the input, it will be stored as a new “word”. Using that word in later input causes the expression compiled into it to be executed.

  • All input is broken into words - everything that is delimited by white-space (except special casess like strings) and each word is executed in sequence. Words are contained in a “dictionary” that is searched for each word encountered in the input. If a word is not found, it is treated as a number

Next
Next

Experiencing K4 First Hand